package com.jhl.seckill.controller;


import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;


@Controller
public class MQController {

    @Autowired
    RabbitTemplate rabbitTemplate;

    /**
     * @param seckillMsg SeckillMsg类型：User+goodsid
     * @return
     */
    @PostMapping("/seckill")
    public String sendTopicSeckillMessage(String seckillMsg) {

        String messageId = String.valueOf(UUID.randomUUID());
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, Object> sendData = new HashMap<>();
//        设置消息id，数据，消息创建时间
        sendData.put("messageId", messageId);
        sendData.put("messageData", seckillMsg);
        sendData.put("createTime", createTime);
        rabbitTemplate.convertAndSend("seckillExchange", "seckill.goods", sendData);
        return "ok";
    }

}
